<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.change-info{
				width: 400px;
				height: 300px;
			}
		</style>
	<base href="<%=request.getContextPath()%>/">
	<script src="js/public/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div class="change-info">
		<table border="1px" cellspacing="0px" cellpadding="" width="100%" height="100%">
			<tr>
				<th colspan="4">学生换房</th>
				
			</tr>
			<tr>
				<td>姓名</td>
				<td id="sname"></td>
				<td>性别</td>
				<td id="sex">男</td>
			</tr>
			<tr>
				<td>班级</td>
				<td id="cid">48期java开发班</td>
				<td>原房间</td>
				<td id="roomno">新南路10号</td>
			</tr>
			<tr>
				<td>新房间</td>
				<td><select name="room-list" id="room">
					<!-- <option value="1">新南路10号</option>
					<option value="2">玉林西路22号</option> -->
				</select></td>
				<td colspan="2"></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="button" name="" id="change" value="确定" /></td>
				<td colspan="2" align="center" id="info"></td>
			</tr>
		</table>
		</div>
	</body>
	<script type="text/javascript">
	//根据id查询学生信息
	$(function(){
		//获取学生id
		var sid=${param.id};
		$.ajax({
			url:"stu/findStuInfo",
			type:"post",
			data:{"id":sid},
			success:function(data){
				$("#sname").html(data.sname);
				if(data.sex==1){
				$("#sex").html("男");
				}
				else if(data.sex==2){
					$("#sex").html("女");
				}
				$("#cid").html(data.clazz.cname);
				$("#roomno").html(data.room.address);
			}
		});
		//查询与当前学生相匹配的所有的房间（性别符合，房间没有住满）
		$.ajax({
			url:"room/findRoomsBySid",
			type:"post",
			data:{"id":sid},
			success:function(data){
				$.each(data,function(index,val){
					if(val.address!=$("#roomno").html()){
					var option=$("<option></option>");
					option.text(val.address);
					option.val(val.id);
					$("#room").append(option);
					}
				})
			}
		});
		//换房,根据学生id更新房间编号
		$("#change").click(function(){
			var roomno=$("#room option:selected").val();
		 $.ajax({
			url:"stu/updateRoom",
			data:{"id":sid,"roomno":roomno},
			type:"post",
			success:function(data){
				if(data==0){
					$("#info").html("<font color=red>换房失败</font>")
				}
				else if(data==1){
					$("#info").html("<font color=green>换房成功</font>")
				}
				else if(data==2){
					$("#info").html("<font color=red>您要换的房间类型与您的性别不匹配!</font>")
				}
				else if(data==3){
					$("#info").html("<font color=red>该房已住满,请选择别的房间。</font>")
				}
			}
		});
			
		});
	})
	
	</script>
</html>
